'use strict';

const Controller = require('egg').Controller;

class StuinsmsgController extends Controller {
    // 创建控制器——POST——/name
    async create() {
        const { ctx } = this;
        const { tea_id, content, progress, stu_id } = ctx.request.body;
        const msg = {
            tea_id,
            content,
            progress,
            stu_id,
        }
        const result = await ctx.service.stuinsmsg.add(msg);

        ctx.status = 201;
        ctx.body = result;
    }
    // // 查询获取数据用控制器——GET——/name?keywords=?&&.....
    async index() {
        const { ctx } = this;
        const keywords = ctx.query.keywords ? ctx.query.keywords : '';
        const pageIndex = ctx.query.pageIndex ? +ctx.query.pageIndex : 1;
        const pageSize = ctx.query.pageSize ? +ctx.query.pageSize : 5;
        console.log('keywords', keywords);
        const result = await ctx.service.stuinsmsg.findList(keywords, pageIndex, pageSize);
        ctx.status = 200;
        ctx.body = result;
    }
    // // // 根据id获取信息的控制器方法——GET——/name/:id
    async show() {
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.stuinsmsg.findById(id);
        ctx.status = 200;
        ctx.body = result;
    }
    // // // 根据id修改对应的数据的控制器方法——PUT——/name/:id
    async update() {
        const { ctx } = this;
        const id = ctx.params.id;
        const {
            content
        } = ctx.request.body;
        const result = await ctx.service.stuinsmsg.modify({ 
            id,
            content
        });
        ctx.body = result;
    }
    // // // // 根据id删除对应的数据的控制器方法——DELETE——/name/:id
    async destroy(){
        const { ctx } = this;
        const id = ctx.params.id;
        const result = await ctx.service.stuinsmsg.remove(id);
        ctx.status = 200;
        ctx.body = result;
    }
    async findByStuPro(){
        const { ctx } = this;
        const stu_id = ctx.query.stu_id;
        const progress = ctx.query.progress;
        console.log(stu_id,progress);
        const result = await ctx.service.stuinsmsg.findByStuidPro(stu_id,progress);
        ctx.status = 200;
        ctx.body = result[0];
    }
    async findBuStuIdDesc(){
        const { ctx } = this;
        const stu_id = ctx.query.stu_id;
        const keywords = ctx.query.keywords ? ctx.query.keywords : '';
        const pageIndex = ctx.query.pageIndex ? +ctx.query.pageIndex : 1;
        const pageSize = ctx.query.pageSize ? +ctx.query.pageSize : 5;
        const result = await ctx.service.stuinsmsg.findbystuiddesc(keywords,pageIndex,pageSize,stu_id);
        ctx.status = 201;
        ctx.body = result;
    }
    async passadd(){
        const { ctx } = this;
        const { tea_id, content, progress, stu_id } = ctx.request.body;
        const msg = {
            tea_id,
            content,
            progress,
            stu_id,
        }
        const result = await ctx.service.stuinsmsg.passAdd(msg);
        console.log(result);
        ctx.body = result;
    }
}
module.exports = StuinsmsgController;